package com.my.screenadapter.bluetooth.print;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

import com.my.screenadapter.R;
import com.my.screenadapter.net.DownLoadActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by 张英杰 on 2017/3/10.
 * http://blog.csdn.net/lasttnt/article/details/27490639
 * http://blog.csdn.net/qq910689331/article/details/53637328
 * http://blog.csdn.net/csdn_zuirenxiao/article/details/50293653
 *
 * 我的有道：
 * http://note.youdao.com/noteshare?id=c112d88e9a693b3a1c7d81cb9a1cf72b
 */
public class PrintShareActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pringshare);
        final String localApkFile = Environment.getExternalStorageDirectory() + File.separator +"abc";
        final String apkFileName=   "MyPrintShare.apk";
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (appIsInstalled(PrintShareActivity.this, "com.dynamixsoftware.printershare")) {
                    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "myimage" + File.separator + "qianyangdan.png");
                    getResources().getDrawable(R.drawable.print_demo);
                    //  ComponentName comp = new ComponentName("com.dynamixsoftware.printershare","com.dynamixsoftware.printershare.ActivityPrintPDF");
//                ComponentName comp = new ComponentName("com.dynamixsoftware.printershare","com.dynamixsoftware.printershare.ActivityPrintDocuments");
                    ComponentName comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintPictures");
                    Intent intent = new Intent();
                    intent.setComponent(comp);
                    intent.setAction("android.intent.action.VIEW");
                    intent.setType("image/png");
                    intent.setData(Uri.fromFile(file));
                    startActivity(intent);
                } else {
                    try {
                        InputStream is = getAssets().open("PrintShare.apk");
                        File file = new File(localApkFile);
                        if (!file.exists()){
                            file.mkdirs();
                        }
                        File apkFile=new File(localApkFile+File.separator+apkFileName);
                        if (apkFile.exists()){
                            file.delete();
                            file.createNewFile();
                        }
                        FileOutputStream fos = new FileOutputStream(apkFile);
                        byte[] temp = new byte[1024];
                        int i = 0;
                        while ((i = is.read(temp)) > 0) {
                            fos.write(temp, 0, i);
                        }
                        fos.close();
                        is.close();
                        DownLoadActivity.installApk(PrintShareActivity.this, apkFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
//                    Intent intent = new Intent(Intent.ACTION_VIEW);
//                    File file = FileUtils.getAssetFileToCacheDir(activity,"xxx.apk");
//                    intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
//                    activity.startActivity(intent);
                }
            }
        });
    }

    //判断apk是否安装
    public static boolean appIsInstalled(Context context, String pageName) {
        try {
            context.getPackageManager().getPackageInfo(pageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
